package com.zlfund.cat.zjgsmk.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zlfund.cat.zjgsmk.R;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenj
 * Created on 2017/8/19
 */

public class RecyclerViewAdapter<T extends BaseBean> extends RecyclerView.Adapter<RecyclerViewHolder> {

    private List<T> mList;
    private Context mContext;
    private IRecyclerViewAdapterHelper<T> mHelper;
    private int mLoadMoreLayoutId;
    private int mLoadErrorLayoutId;
    private int mLoadEmptyLayoutId;

    public RecyclerViewAdapter(Context context, IRecyclerViewAdapterHelper<T> helper) {
        mContext = context;
        mHelper = helper;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        if (viewType == Type.TYPE_LOAD_MORE) {
            v = LayoutInflater.from(mContext).inflate(mLoadMoreLayoutId, parent, false);
        } else if (viewType == Type.TYPE_EMPTY) {
            v = LayoutInflater.from(mContext).inflate(mLoadEmptyLayoutId, parent, false);
        } else if (viewType == Type.TYPE_LOAD_ERROR) {
            v = LayoutInflater.from(mContext).inflate(mLoadErrorLayoutId, parent, false);
        } else {
            v = LayoutInflater.from(mContext).inflate(mHelper.getLayoutId(viewType), parent, false);
        }
        RecyclerViewHolder holder = new RecyclerViewHolder(v);
        holder.setType(viewType);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        mHelper.initViews(holder, position, mList.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            if (mList.get(position).isHasMore()) {
                mLoadMoreLayoutId = mList.get(position).getLoadMoreLayoutId();
                return Type.TYPE_LOAD_MORE;
            }
        }
        if (mList.size() == 1) {
            if (mList.get(0).isLoadEmpty()) {
                mLoadEmptyLayoutId = mList.get(0).getLoadEmptyLayoutId();
                return Type.TYPE_EMPTY;
            }
            if (mList.get(0).isLoadError()) {
                mLoadErrorLayoutId = mList.get(0).getLoadErrorLayoutId();
                return Type.TYPE_LOAD_ERROR;
            }
        }
        return mHelper.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void setList(List<T> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void addList(List<T> list) {
        if (mList == null) mList = new ArrayList<>();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void addMoreList(List<T> list, boolean loadMore, int loadMoreResId) {
        if (mList == null) mList = new ArrayList<>();
        if (loadMore) {
            if (mList.size() > 0 && mList.get(mList.size() - 1).isHasMore()) {
                mList.remove(mList.size() - 1);
            }
        }
        mList.addAll(list);
        if (loadMore) {
            if (!list.isEmpty()){
                T t = list.get(0);
                t.setHasMore(true);
                t.setLoadMoreLayoutId(loadMoreResId);
                mList.add(t);
            }
        }
        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public void setNoData(T t,int errorLayoutId){
        t.setLoadError(true);
        t.setLoadErrorLayoutId(errorLayoutId);
        mList.clear();
        mList.add(t);
        notifyDataSetChanged();
    }

    public void setEmptyData(T t,int emptyLayoutId){
        t.setLoadEmpty(true);
        t.setLoadEmptyLayoutId(emptyLayoutId);
        mList.clear();
        mList.add(t);
        notifyDataSetChanged();
    }

    public void clearLoadMore() {
        if (mList == null) return;
        if (mList.size() > 0 && mList.get(mList.size() - 1).isHasMore()) {
            mList.remove(mList.size() - 1);
            notifyDataSetChanged();
        }
    }

}
